<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoBunddle extends Controller_Layout {
	public $auth_required = TRUE;

	public function action_index()
	{
		$bunddle = ORM::factory("bunddle");
		$result_bunddle = $bunddle->where("status","=","A")->find_all();
		
	}
	
	public function action_insertPage(){
		$view = View::factory("pages/bo/dish_bunddle_add_page");
		$dish = ORM::factory("dish");
		$supplier_id = $_GET["supid"];
		$result_dish = $dish->where("supplierid","=",$supplier_id)->and_where("status","=","A")->find_all();
		$view->result_dish = $result_dish; 
		exit($view->render());
	}

	public function action_insert(){
		$bunddle = ORM::factory("bunddle");
		$fileUtil = FileUtil::Factory();
		$fileName = $fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'bunddle');
		if ($fileName != FALSE){
			$fileUtil->setFilePath($fileName,'picture');
		}
		$_POST["status"] = "A";
		if($bunddle->values($_POST)->check()){
			ORM::factory("supplier",$_POST["supplierid"])->updateModifyTime();
			$bunddle->save();
		}
	}

	public function action_updatePage(){
		$view = View::factory("pages/bo/dish_bunddle_edit_page");
		
		$bunddle = ORM::factory("bunddle",$_GET["bunddleid"]);
		
		
		$dish = ORM::factory("dish");
		$supplier_id = $_GET["supid"];
		
		$result_dish = $dish->where("supplierid","=",$supplier_id)->and_where("status","=","A")->find_all();
		$view->result_dish = $result_dish; 
		$view->result = $bunddle;
		exit($view->render());
	}
	
	public function action_update(){
		$bunddle = ORM::factory("bunddle",$_POST["id"]);
		$bunddle->name = $_POST["name"];
		$bunddle->price = $_POST["price"];
		$bunddle->desc = $_POST["desc"];
		$supplier_id = $bunddle->supplierid;
		
		$fileUtil = FileUtil::Factory();
		$fileUtil->setFilePath($fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'bunddle'),'picture');
		if (!empty($_POST['picture'])){
			if ($_POST['picture']!=$bunddle->picture){
				if (!empty($bunddle->picture)){
					$fileUtil->deleteFile($bunddle->picture);
				}
				$bunddle->picture = $_POST['picture'];
			}
		}		
		
		if($bunddle->save()){
			$bunddle = ORM::factory("bunddle");
			$result_bunddle = $bunddle->where("status","=","A")->and_where("supplierid","=",$_POST["supplierid"])->order_by("name","ASC")->find_all();
			$view = View::factory("pages/bo/dish_bunddle_page");
			$view->result_bunddle = $result_bunddle;
			$view->result_supplier_name = ORM::factory("supplier",$supplier_id)->name;
			ORM::factory("supplier",$supplier_id)->updateModifyTime();
			exit($view->render());
		}
		
	}
	
	public function action_show(){
		$bunddle = ORM::factory("bunddle");
		$supplier_id = $_GET["supid"];
		$result_bunddle = $bunddle->where("status","=","A")->and_where("supplierid","=",$supplier_id)->order_by("name","ASC")->find_all();
		$view = View::factory("pages/bo/dish_bunddle_page");
		$view->result_bunddle = $result_bunddle;
		$view->result_supplier_name = ORM::factory("supplier",$supplier_id)->name;
		exit($view->render());
	}
	
	public function action_deleteAll(){
		$bunddle = ORM::factory("bunddle");
		$bunddle->status = "I";
		$ids = $_POST["ids"];
		foreach (split(",", $ids) as $id){
			if (!empty($id)){
				$bunddle->or_where("id","=",$id);
				if (empty($supplierId)){
					$supplierId = ORM::factory("bunddle",$id)->supplierid;
				}
			}
		}
		$bunddleItem = ORM::factory("bunddleItem");
		$bunddleItem->status = "I";
		foreach (split(",", $ids) as $id){
			if (!empty($id)){
				$bunddleItem->or_where("bunddleid","=",$id);
			}
		}
		
		if($bunddle->save_all()&&$bunddleItem->save_all()){
			ORM::factory("supplier",$supplierId)->updateModifyTime();
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除套餐成功";
			exit($view->render());
		}
	}
}
